package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.service.SetmealService;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    /*
        根据分类id查询套餐
    */
    @Override
    @Cacheable(cacheNames = "setmealCache",key = "#categoryId")
    public List<Setmeal> selectByCategoryId(Long categoryId) {
        return setmealMapper.selectByCategoryId(categoryId);
    }
    /*
    * 根据套餐id查询套餐包含的菜品
    * */
    @Override
    public List<DishItemVO> selectBySetmealId(Long id) {
        return setmealDishMapper.selectBySetmealId(id);
    }

    /*
    * 新增套餐
    * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void save(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //保存套餐基本信息
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        //保存套餐菜品关系
        setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes(),setmealId);
    }

    /*
    * 分页查询
    * */
    @Override
    public PageResult pageList(SetmealPageQueryDTO setmealPageQueryDTO) {
        //启用分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> res = (Page<Setmeal>) setmealMapper.page(setmealPageQueryDTO.getName(),setmealPageQueryDTO.getCategoryId(),setmealPageQueryDTO.getStatus());
        return new PageResult(res.getTotal(),res.getResult());
    }

    /*
    * 根据id查询套餐（包含套餐菜品数据）
    * */
    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealIdWithSetmealDish(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /*
    * 根据ids批量删除套餐
    * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void deleteByIds(List<Long> ids){
        //统计起售中套餐数量
        Integer countSetmealOnSale = setmealMapper.countSetmealOnSaleByIds(ids);
        if(countSetmealOnSale > 0){
            throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /*
    *
    * 起售停售套餐
    * */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void changeStatus(Integer status,Long id){
        //查询套餐中未起售的菜品数量
        if(status == 1){
            Integer countNotOnSale = setmealDishMapper.countNotOnSaleBySetmealId(id);
            if(countNotOnSale > 0){
                throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id).build();
        setmealMapper.update(setmeal);
    }

    /*
    * 修改套餐
    * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void update(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //保存套餐基本信息
        setmealMapper.update(setmeal);
        //保存套餐菜品关系
        setmealDishMapper.deleteBySetmealIds(Arrays.asList(setmealDTO.getId()));
        setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes(),setmealDTO.getId());
    }
}
